package info.vanbeem.WikiMindDesktop;

import java.io.IOException;
import java.util.EnumSet;

import org.eclipse.swt.browser.Browser;

import com.admc.jcreole.CreoleParseException;
import com.admc.jcreole.JCreolePrivilege;

class FileViewer {

	public FileViewer(MainWindow mainwindow) {
		this.mainWindow = mainwindow;
		browser = mainwindow.getBrowser();
		settings = mainwindow.getSettings();
	}

	private final MainWindow mainWindow;
	private final Browser browser;
	private final Settings settings;
		
	void setText(final String text) {
		if (text.length() > 0) {
			final WikiParser parser = (settings.getTemplate() == null) ? new WikiParser() : new WikiParser(settings.getTemplate());
			parser.setPrivileges(EnumSet.allOf(JCreolePrivilege.class));
			try {
				browser.setText(parser.parse(text));
			} catch (IOException e) {
				browser.setText(e.toString());
			}
			catch (CreoleParseException e) {
				browser.setText(e.toString());
			}
		}
		else {
			browser.setText(text);
		}
	}
}
